FROM nvidia/cuda:8.0-cudnn5-devel-ubuntu16.04


# change software sources
RUN sed -ie 's/archive.ubuntu.com/ftp.sjtu.edu.cn/g' /etc/apt/sources.list

RUN apt-get update
RUN apt-get install -y software-properties-common vim

RUN add-apt-repository ppa:jonathonf/python-3.6

RUN apt-get update --fix-missing

RUN apt-get install -y apt-utils
RUN apt-get install -y --no-install-recommends \
        build-essential \
        cmake \
        git \
        wget \
        pkg-config \
        libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev \
        libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \
        libxvidcore-dev libx264-dev \
        libatlas-base-dev gfortran \
        libatlas-base-dev \
        libboost-all-dev \
        libgflags-dev \
        libgoogle-glog-dev \
        libhdf5-serial-dev \
        libleveldb-dev \
        liblmdb-dev \
        libsnappy-dev \
        python3 ca-certificates python3-dev \
        autoconf automake libtool curl make g++ unzip \
        ssh iputils-ping vim python3-tk \
	libgtest-dev mysql-client libmysqlclient-dev \
	libtesseract-dev tesseract-ocr libtesseract3 tesseract-ocr-eng tesseract-ocr-chi-sim libleptonica-dev \
	htop

# install python 3.6
RUN apt-get install -y build-essential python3.6 python3.6-dev python3-pip


# install pip
RUN python3.6 -m pip install pip --upgrade
RUN python3.6 -m pip install wheel


ENV CAFFE_ROOT=/opt/caffe
WORKDIR $CAFFE_ROOT

# build opencv
RUN pip3 --version
RUN pip3 install -U numpy scipy python-dateutil xlrdn seaborn
RUN pip3 install -U pyinotify python-jose peewee flask_mysqldb  pytesseract tesseract tesserocr

RUN git clone https://github.com/opencv/opencv.git
RUN cd opencv && \
        mkdir build && \
        cd build && \
        cmake -D CMAKE_BUILD_TYPE=RELEASE -DPYTHON3_EXECUTABLE=/usr/bin/python3.6 .. \
        && make -j$(nproc) && \
        make install && \
        cd ../.. && rm -rf opencv
# test opencv for python
RUN cd / && python3.6 -c 'import cv2'

# build and install protobuf for c++ and python
RUN git clone https://github.com/google/protobuf.git
RUN apt-get install libcurl3
RUN cd protobuf && ./autogen.sh && ./configure &&\
        make -j $(nproc) && make -j $(nproc) check && make install && ldconfig
RUN rm protobuf -rf

# install caffe
RUN git clone https://github.com/BVLC/caffe.git .
RUN pip3 install -r python/requirements.txt
RUN pip3 install -U redis celery pydot 
RUN git clone https://github.com/NVIDIA/nccl.git && cd nccl && make -j $(nproc) install && cd .. && rm -rf nccl

# build caffe
RUN ln -rs /usr/lib/x86_64-linux-gnu/libboost_python-py35.so /usr/lib/x86_64-linux-gnu/libboost_python3.so

ADD Makefile.config Makefile.config
RUN make -j $(nproc) && make pycaffe && make dist
ENV PYCAFFE_ROOT $CAFFE_ROOT/python
ENV PYTHONPATH $PYCAFFE_ROOT:$PYTHONPATH
ENV PATH $CAFFE_ROOT/build/tools:$PYCAFFE_ROOT:$PATH
RUN echo "$CAFFE_ROOT/build/lib" >> /etc/ld.so.conf.d/caffe.conf && ldconfig

# test caffe
RUN pip3 install -U python-dateutil 
RUN cd / && python3.6 -c 'import caffe'

# install jupyter
RUN apt-get install -y texlive-full
RUN pip3 install flask coloredlogs jupyter

RUN jupyter notebook --generate-config --allow-root

#install tensor flow
RUN pip3 install -U tensorflow-gpu sklearn xlrd

RUN pip3 install -U lmdb

EXPOSE 8888 8080 

# clean up
RUN rm -rf /var/lib/apt/lists/*

ADD run.sh /run.sh

CMD /run.sh


